package zhengdaoyang.settings.dao;

import com.mysql.cj.protocol.Resultset;
import zhengdaoyang.settings.domain.Goods;
import zhengdaoyang.settings.domain.User;
import zhengdaoyang.utils.MyDbUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao{
    @Override
    public int login(Map<String, String> map) {

        String name = map.get("loginAct");
        String pass= map.get("loginPwd");
        System.out.println("loginact="+name+" "+ pass);
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "select * from users where username=? and password=?";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,pass);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        try {
            rs =  ps.executeQuery();
            rs.next();
            na =  rs.getString("username");

        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }
        System.out.println("-------na="+na);

        if(na != null){
            MyDbUtil.close(connection,ps, rs);
            return 1;
        } else{
            MyDbUtil.close(connection,ps, rs);
            return -1;
        }

    }

    @Override
    public User getUserList() {
        return null;
    }

    @Override
    public int findid(String name) {
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "select uid from users  where username =? ";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        int id=0;
        try {
            rs =  ps.executeQuery();
            while(rs.next()){
              id= rs.getInt("uid");
            }
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕=");
        return id;
    }

    @Override
    public int updatePassword(String name, String password) {
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "update  users set password=? where username=?";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,name);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        int  kk =0;
        try {
           kk  =  ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }
        return kk;
    }

    @Override
    public int insertuser(String name, String password) {
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection111111="+connection);

        /*预编译*/
        String sql = "insert into users(username,password) values(?,?)";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,password);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        int  kk =0;
        try {
            kk  =  ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }
        return kk;
    }
}
